import time

from xjlibrary.newthreading.MThreadingRun import MThreadingRun
from xjlibrary.newthreading.mthreading import ThreadManager
from xjlibrary.our_file_dir import BaseDir


class ParseBigJson(object):

    def set_localVal(self,localVal):
        self.localVal = localVal

    def print(self, threadval, json, count):
        # print(json)
        print("&&&&&&&&&&&&&&&&&")
        print(count)
        queue = threadval.result_queue
        queue.put((json, count))
        self.localVal.x = 100
        self.text()
        time.sleep(20)

    def text(self):
        print("*************************")
        print(self.localVal.x)
        print("*************************")





class ParseThreadRun(MThreadingRun):

    def __init__(self, num):
        super(ParseThreadRun, self).__init__(num)
        self.down = ParseBigJson()
        self.count = 0
        self.down.set_localVal(self.thread_pool.localVal)

    def getTask(self, *args, **kwargs):
        for filepath in BaseDir.get_dir_all_files(r"D:\download\TandBook\download\big_json"):
            file = open(filepath, encoding='utf-8')
            while True:
                self.count += 1
                print("input:{}".format(self.count))
                line = file.readline()
                self.add_job(self.func, line, self.count)
                if not line:
                    break

    def setTask(self, results=None, *args, **kwargs):
        pass
        # return "break"

    def setProxy(self, proxysList=None):
        return "break"

    def is_break(self):
        return True

    def thread_pool_hook(self, thread_pool_dicts: dict, dictsfull: dict, *args, **kwargs):
        if dictsfull["threadname"] in ["task_thread_1", "proxy_thread_1"]:
            thread = dictsfull["thread"]
            if not isinstance(thread, ThreadManager):
                raise TypeError("线程对象不是ThreadManager")
            thread.is_restart = False

    def fun(self, threadval, *args, **kwargs):
        self.down.print(threadval, args[0], args[1])

    def dealresult(self, *args, **kwargs):
        for value, count in self.results:
            print(len(value))
            print("****{}".format(str(count)))


def main():
    threadrun = ParseThreadRun(20)
    threadrun.run(model=1)


if __name__ == "__main__":
    main()
